home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1991 / number6 / phonbook.asc < prev    next >
Text File  |  1991-11-08  |  5KB  |  202 lines

  1. ViewManager subclass: #PhoneBook
  2.   instanceVariableNames:    'people selectedPerson '
  3.   classVariableNames: ''
  4.   poolDictionaries: ''
  5.  
  6. Object subclass: #Person
  7.   instanceVariableNames:    'name phoneNumber '
  8.   classVariableNames: ''
  9.   poolDictionaries: ''
  10.  
  11. PhoneBook methods
  12.  
  13. names: listBox
  14.   "Set the contents of the names listBox"
  15.  
  16.   listBox contents: (self alphabetizedArrayOfNames)
  17.  
  18. open
  19.   "Open the receiver's window, and begin
  20.    processing user input.  Double-clicking a
  21.    name will print that person's name and
  22.    number"
  23.  
  24.   self label: 'Phone Book'.
  25.  
  26.   self addSubpane:
  27.     ((ListBox new)
  28.        owner: self;
  29.        when: #getContents perform: #names: ;
  30.        when: #getMenu perform: #namesMenu: ;
  31.        when: #doubleClickSelect perform: #setSelectedPerson: ;
  32.             framingRatio:
  33.                 (Rectangle leftTopUnit extentFromLeftTop: 1@(3/4))).
  34.  
  35.     self addSubpane:
  36.         ((StaticText new)
  37.             centered;
  38.             owner: self;
  39.             when: #getContents perform: #information: ;
  40.             framingRatio:
  41.                 ((Rectangle leftTopUnit rightAndDown: 0@(3/4))
  42.                                   extentFromLeftTop: 1@(1/4))).
  43.  
  44.     self openWindow.
  45.  
  46. selectedPerson
  47.   "Get the value of selectedPerson."
  48.  
  49.   ^selectedPerson
  50.  
  51. selectedPerson: aPerson
  52.   "Set the value of selectedPerson."
  53.  
  54.   ^selectedPerson := aPerson.
  55.  
  56. printAlphabetically
  57.   "Print alphabeticlly all the people in the receiver's book
  58.    on the Transcript"
  59.  
  60.   | names |
  61.  
  62.   Transcript nextPutAll: 'My Phonebook:'; cr; cr.
  63.  
  64.   names := self alphabetizedArrayOfNames.
  65.   names do:   [:aName |
  66.     (self people at: aName) printOn: Transcript.
  67.      Transcript cr
  68.   ].
  69.  
  70. information: infoPane
  71.   "Set the contents of the infoPane to be the printString
  72.    of the selectedPerson."
  73.  
  74.   selectedPerson isNil ifTrue:    [
  75.     infoPane contents: ''
  76.   ] ifFalse:  [
  77.       infoPane contents: self selectedPerson printString
  78.   ].
  79.  
  80. setSelectedPerson: namesPane
  81.   "Private - set the selectedPerson to be
  82.    the person who's name was double-clicked"
  83.  
  84.   selectedPerson := (self people at: namesPane selectedItem).
  85.   self changed: #information:
  86.  
  87. removePerson
  88.   "remove the selectedPerson from the phoneBook"
  89.  
  90.   self selectedPerson isNil ifTrue:   [
  91.     MessageBox message: 'You must select a person to remove'.
  92.   ] ifFalse:  [
  93.     self people removeKey: self selectedPerson name.
  94.     self selectedPerson: nil;
  95.           changed: #names: ;
  96.           changed: #information:
  97.   ].
  98.  
  99. namesMenu: namesPane
  100.   "Set the namesPane menu"
  101.  
  102.   namesPane setMenu:
  103.     ((Menu new)
  104.       title: '&PhoneBook';
  105.       owner: self;
  106.       appendItem: '&Add Person...' selector: #addPerson ;
  107.       appendItem:
  108.         '&Print alphabetically' selector: #printAlphabetically)
  109.  
  110. initialize
  111.   "Initialize the receiver to be a default phoneBook.
  112.    Don't forget to call super initialize, to initialize
  113.    the ViewManager representation we're inheriting"
  114.  
  115.   super initialize.
  116.   people := Dictionary new.
  117.  
  118. alphabetizedArrayOfNames
  119.   "Return an alphabetized array of all the names
  120.    in the receiver's book"
  121.  
  122.   ^self people keys asSortedCollection asArray
  123.  
  124. addPerson
  125.   "Add a person to the receiver's phone book"
  126.  
  127.   | aPerson |
  128.  
  129.   aPerson := (Person new) fillFromKeyboard.
  130.  
  131.   aPerson notNil ifTrue:      [
  132.     self people at: aPerson name put: aPerson.
  133.     self changed: #names:
  134.   ].
  135.  
  136. people
  137.   "Get the value of people."
  138.  
  139.   ^people
  140.  
  141.  
  142.  
  143. Person methods
  144.  
  145. phoneNumber: aSymbol
  146.   "Private - Set the value of phoneNumber."
  147.  
  148.     ^phoneNumber := aSymbol
  149.  
  150. fillFromKeyboard
  151.   "Fill the receiver's fields from the keyboard.
  152.    Return nil if the user pressed cancel for any
  153.    field; return self otherwise"
  154.  
  155.   | aName aPhoneNumber |
  156.  
  157.   aName := Prompter prompt: 'Name?' default: self name.
  158.   aName isNil ifTrue: [
  159.     ^nil
  160.   ].
  161.  
  162.   aPhoneNumber := Prompter prompt: 'Phone number?' default: self phoneNumber.
  163.   aPhoneNumber isNil ifTrue:   [
  164.     ^nil
  165.   ].
  166.  
  167.   ^self
  168.     name: aName asSymbol;
  169.     phoneNumber: aPhoneNumber asSymbol;
  170.     yourself.
  171.  
  172. name: aSymbol
  173.   "Private - Set the value of name."
  174.  
  175.     ^name := aSymbol
  176.  
  177. phoneNumber
  178.   "Get the value of phoneNumber."
  179.  
  180.   ^phoneNumber
  181.  
  182. initialize
  183.   "Initialize the receiver to be a default Person"
  184.  
  185.   self
  186.     name: 'a Person' asSymbol;
  187.     phoneNumber: '411' asSymbol.
  188.  
  189. printOn: aStream
  190.   "Print the receiver's representation on aStream
  191.    like Jon Doe, (514) 555-1212"
  192.  
  193.   self name printOn: aStream.
  194.   aStream nextPut: $, ;
  195.           nextPut: $ .
  196.   self phoneNumber printOn: aStream.
  197.  
  198. name
  199.   "Get the value of name."
  200.  
  201.   ^name
  202.